package com.yjc.hire.controller.saas;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.yjc.hire.VO.CompanyInfoVO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.service.CompanyService;
import com.yjc.pojo.Company;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/8/13
 * </p>
 */

@RestController
@RequestMapping("saas")
public class SaasCompanyController extends BaseInfoProperties {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CompanyService companyService;

    @PostMapping("moreInfo")
    public GraceJSONResult saasMoreInfo(String companyId){
        CompanyInfoVO companyInfo = getSaasCompanyInfo(companyId);
        return GraceJSONResult.ok(companyInfo);
    }

    private CompanyInfoVO getSaasCompanyInfo(String companyId){
        if (StringUtils.isBlank(companyId))return null;
        String key = REDIS_COMPANY_MORE_INFO + ":" + companyId;
        String companyJson = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(companyJson)){
            Company company = companyService.getCompanyById(companyId);
            if (company == null){
                return null;
            }
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            BeanUtils.copyProperties(company, companyInfoVO);
            stringRedisTemplate.opsForValue().set(key,new Gson().toJson(companyInfoVO), 60);
            return companyInfoVO;
        }else {
            return new Gson().fromJson(companyJson, CompanyInfoVO.class);
        }
    }
}
